/* Copyright 2011 Brian Cairns
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.wsnetwork.reforger;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

public class ReforgerApplet extends Applet implements ActionListener  {
    private static final long serialVersionUID = 7687889740658301342L;

    private Button okButton;
    private TextField urlField;
    private TextArea resultField;
    private PrintStream stream;

    private static Font getConsoleFont() {
        return new Font("Monospaced", Font.PLAIN, 12);
    }

    @Override
    public void init()
    {
        Font consoleFont = getConsoleFont();

        setSize(800, 500);
        okButton = new Button("Calculate");
        urlField = new TextField("", 80);
        resultField = new TextArea(24, 100);
        resultField.setFont(consoleFont);
        resultField.setEditable(false);
        resultField.setText("Enter your Armory URL above and click the calculate button.");
        resultField.setBackground(Color.black);
        resultField.setForeground(Color.white);

        add(urlField);
        add(okButton);
        add(resultField);

        // Create an output stream that writes to the result text box
        OutputStream output = new OutputStream()
        {
            private StringBuilder string = new StringBuilder();
            @Override
            public void write(int b) throws IOException {
                string.append((char) b );
                if ((char) b == '\n') {
                    resultField.append(string.toString());
                    string = new StringBuilder();
                }
            }
        };
        stream = new PrintStream(output, true);

        okButton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        Runnable reforger = new ReforgerRunnable(urlField.getText(), stream);
        Thread thread = new Thread(reforger);
        thread.start();
    }

    private class ReforgerRunnable implements Runnable {
        private final String url;
        private final PrintStream outputStream;

        private ReforgerRunnable(String url, PrintStream outputStream) {
            this.url = url;
            this.outputStream = outputStream;
        }

        @Override public void run() {
            try {
                urlField.setEnabled(false);
                okButton.setEnabled(false);
                resultField.setText("");
                Reforger reforger = new Reforger(outputStream, new StandardUrlRetriever());
                reforger.runReforging(url);
            } catch (Exception e) {
                e.printStackTrace(outputStream);
            } finally {
                urlField.setEnabled(true);
                okButton.setEnabled(true);
            }
        }

    }
}
